package com.koron.css2.clientCenter.impl;


import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.ValvePostMsgBean;
import com.koron.css2.clientCenter.mapper.ValvePubMapper;
import com.koron.css2.clientCenter.mapper.ValveUserMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cd
 * 转办人员列表
 */
public class ValveTransferAccountList implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ValvePubMapper orgUserMapper = factory.getMapper(ValvePubMapper.class);
            ValveUserMapper valveUserMapper = factory.getMapper(ValveUserMapper.class);
            List<ValvePostMsgBean> list = orgUserMapper.selectList();
            Map<String, List<String>> postMap = list.stream().collect(Collectors.groupingBy(data->data.getUserAccount(),Collectors.mapping(ValvePostMsgBean::getPostName,Collectors.toList())));

            List<String> accountList = list.stream().map(ValvePostMsgBean::getUserAccount).collect(Collectors.toList());
            // 查的是org_user表
            List<ValvePostMsgBean> msgBeans = valveUserMapper.selectListByAccount(accountList);

            List<ValvePostMsgBean> legworkList = new ArrayList<>();
            List<ValvePostMsgBean> meterReaderList = new ArrayList<>();
            for (ValvePostMsgBean bean : msgBeans) {
                if (null != postMap.get(bean.getUserAccount()) &&  postMap.get(bean.getUserAccount()).size()>0) {
                if (postMap.get(bean.getUserAccount()).contains("抄表员")) {
                    bean.setPostName("抄表员");
                    meterReaderList.add(bean);
                }
                if (postMap.get(bean.getUserAccount()).contains("外勤人员")) {
                    bean.setPostName("外勤人员");
                    legworkList.add(bean);
                }
                }
            }

            HashMap<String, Object> map = new HashMap<>();
            //外勤人员
            map.put("legworkList", legworkList);
            //抄表人员
            map.put("meterReaderList", meterReaderList);
            @SuppressWarnings("rawtypes")
            MessageBean<HashMap> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", HashMap.class);
            info.setData(map);
            return info;
        } catch (Exception e) {
            logger.error("转办人员列表查询" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "转办人员列表查询失败", null);
        }
    }
}
